package com.yangjun.controller.waresAnalyse;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.yangjun.dao.impl.waresAnalyse.WaresAnalyseDaoImpl;
import com.yangjun.dao.mybatis2.mapper.common.CommonMapper;
import com.yangjun.dao.mybatis2.mapper.waresAnalyse.WaresAnalyseMapper;
import com.yangjun.entity.businessAnalysis.logisticsAnalysis.HostSaleGroupDetailsEntity;
import com.yangjun.entity.businessAnalysis.logisticsAnalysis.HostSaleSigleEntity;
import com.yangjun.entity.waresAnalyse.BuyInputEntity;
import com.yangjun.entity.waresAnalyse.DayOutRateEntity;
import com.yangjun.entity.waresAnalyse.GoodsTableEntity;
import com.yangjun.entity.waresAnalyse.MatchLinesEntity;
import com.yangjun.entity.waresAnalyse.MonthOutRateEntity;
import com.yangjun.entity.waresAnalyse.SelectWareNameEntity;
import com.yangjun.entity.waresAnalyse.SupplierInputEntity;
import com.yangjun.entity.waresAnalyse.VisWareRankEntity;
import com.yangjun.entity.waresAnalyse.VisWareRankTotalEntity;
import com.yangjun.entity.waresAnalyse.WareSummaryEntity;
import com.yangjun.entity.waresAnalyse.WaresAnalyseDay;
import com.yangjun.entity.waresAnalyse.YearBuyInputEntity;
import com.yangjun.utils.AjaxJson;
import com.yangjun.utils.DvDateUtils;
import com.yangjun.utils.JSONUtils;
import com.yangjun.utils.RedisClientLocalUtils;
import com.yangjun.utils.export.ExportExcel;

import jxl.Workbook;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;



/**
 * 商品分析
 * @author lijianhua
 *
 */
@RestController
@RequestMapping("/waresanaysis")
public class WaresAnalyseController {
	
	@Autowired
	public WaresAnalyseMapper mapper;
	
	
	@Autowired
	public CommonMapper commonMapper;
	
	@Autowired
	WaresAnalyseDaoImpl dao;
	
	private static RedisClientLocalUtils rc = new RedisClientLocalUtils();
	
	/**
	 * http://192.168.7.32:8080/waresanaysis/getSearchContent?search=manufacturername
	 * 文本框搜索数据
	 * @param response
	 * @param search
	 * @return
	 */
	 @RequestMapping("/getSearchContent")
	 @ResponseBody
	 public AjaxJson getSearchContent(HttpServletResponse response,
			 @RequestParam(value = "search",required = true)String search){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 AjaxJson j = new AjaxJson();
		 Map<String, Object> attributes = Maps.newHashMap();
		 String cachecontentList = rc.getValue(search);
		 if(StringUtils.isNotBlank(cachecontentList) && !cachecontentList.equals("[]")){
			 try {
//				    System.err.printf("缓存 %s 有数据",search);
					List strToObj = JSONUtils.strToObj(cachecontentList, List.class);
					attributes.put("content",  strToObj);
				 } catch (Exception e) {
					e.printStackTrace();
				 } 
		 }else{
			 System.err.printf("缓存   %s 无数据 请检查缓存",search);
			 List<String> contentList = mapper.getSearchContent(search);
			 try {
				rc.setKeyValue(search, JSONUtils.objToStr(contentList));
			} catch (IOException e) {
				e.printStackTrace();
			}
			 attributes.put("content", contentList);
		 }
		 j.setAttributes(attributes);
		 return j;
	 }
	 
	 /**
	  * http://192.168.7.32:8080/waresanaysis/getSearchContentSuppliername
	  * @param response
	  * @return
	  */
	 @RequestMapping("/getSearchContentSuppliername")
	 @ResponseBody
	 public AjaxJson getSearchContentSuppliername(HttpServletResponse response){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 AjaxJson j = new AjaxJson();
		 Map<String, Object> attributes = Maps.newHashMap();
		 String cachecontentList = rc.getValue("suppliername");
		 if(StringUtils.isNotBlank(cachecontentList) && !cachecontentList.equals("[]")){
			 try {
//				    System.err.printf("缓存 %s 有数据","供应商");
				 List strToObj = JSONUtils.strToObj(cachecontentList, List.class);
				 attributes.put("content",  strToObj);
			 } catch (Exception e) {
				 e.printStackTrace();
			 } 
		 }else{
			 System.err.printf("缓存   %s 无数据 请检查缓存","供应商");
			 List<String> contentList = mapper.getSearchContentSuppliername();
			 try {
				 rc.setKeyValue("suppliername", JSONUtils.objToStr(contentList));
			 } catch (IOException e) {
				 e.printStackTrace();
			 }
			 attributes.put("content", contentList);
		 }
		 j.setAttributes(attributes);
		 return j;
	 }
	 
	 
	 /**
	  * http://192.168.7.32:8080/waresanaysis/getKsType
	  * @param response
	  * @return
	  */
	 @RequestMapping("/getKsType")
	 @ResponseBody
	 public AjaxJson getKsType(HttpServletResponse response){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 AjaxJson j = new AjaxJson();
		 Map<String, Object> attributes = Maps.newHashMap();
	     attributes.put("operation", mapper.getKsType("运营科室"));
	     attributes.put("finance", mapper.getKsType("财务科室"));
		 j.setAttributes(attributes);
		 return j;
	 }
	 
	 /**
	  *  http://192.168.7.32:8080/waresanaysis/getGoodsTableNew?cookieid=92dc1bf2-30cd-4ca5-a7d6-f3ebeaef1bf9
	  * @param response
	  * @param cookieid
	  * @param condition
	  * @return
	  */
	 @RequestMapping("/getGoodsTableNew")
	 @ResponseBody
	 public AjaxJson  getGoodsTableNewPage(HttpServletResponse response,
			 @RequestParam(value = "pageNum", required = true) String pageNum,
		     @RequestParam(value = "pageSize", required = true) String pageSize,
		     @RequestParam(value = "ordersource", required = false) String ordersource,
			 @RequestParam(value = "waregeneralname",required = false)String waregeneralname,
			 @RequestParam(value = "brandname",required = false)String brandname,
			 @RequestParam(value = "manufacturername",required = false)String manufacturername,
			 @RequestParam(value = "suppliername",required = false)String suppliername,
			 @RequestParam(value = "warename",required = false)String warename,
			 @RequestParam(value = "cookieid",required = true)String cookieid,
			 @RequestParam(value = "condition1",required = false)String condition1,
			 @RequestParam(value = "condition2",required = false)String condition2,
			 @RequestParam(value = "order",required = false)String order
			 ){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
//		 System.out.println("condition1==="+condition1);
//		 System.out.println("condition2==="+condition2);
		 System.out.println("平台==="+ordersource);
		 
		 try {
			 
			 if(StringUtils.isBlank(order)){
				 order = "stockqty desc";
			 }
			 
			 if(StringUtils.isNotBlank(condition1)){
				 condition1 = "and expireddate - trunc(sysdate) <="+condition1;
			 }
			 if(StringUtils.isNotBlank(condition2)){
				 condition2 = java.net.URLDecoder.decode(condition2,"utf-8");
			 }
			 if(StringUtils.isNotBlank(waregeneralname)){
				 waregeneralname = java.net.URLDecoder.decode(waregeneralname,"utf-8");
				 condition2 = condition2+ " and waregeneralname like '%"+waregeneralname+"%'";
			 }
			 if(StringUtils.isNotBlank(brandname)){
				 brandname = java.net.URLDecoder.decode(brandname,"utf-8");
				 condition2 = condition2+ " and brandname like '%"+brandname+"%'";
			 }
			 if(StringUtils.isNotBlank(manufacturername)){
				 manufacturername = java.net.URLDecoder.decode(manufacturername,"utf-8");
				 condition2 = condition2+ " and manufacturername like '%"+manufacturername+"%'";
			 }
			 if(StringUtils.isNotBlank(warename)){
				 warename = java.net.URLDecoder.decode(warename,"utf-8");
				 condition2 = condition2+ " and warename like '%"+warename+"%'";
			 }
			 if(StringUtils.isNotBlank(suppliername)){
				 suppliername = java.net.URLDecoder.decode(suppliername,"utf-8");
				 condition2 = condition2+ " and suppliername like '%"+suppliername+"%'";
			 }

		 } catch (UnsupportedEncodingException e) {
			 e.printStackTrace();
		 }
		 AjaxJson j = new AjaxJson();
		 Map<String, Object> attributes = Maps.newHashMap();
//		 System.err.println(waregeneralname+brandname+manufacturername+suppliername+warename);
//		 System.err.println("condition1=="+condition1);
//		 System.err.println("condition2=="+condition2);
//		 condition1="and expireddate - trunc(sysdate) <= 100";
//		 condition2="and cycle_type=30 and fa_dept in ('OTC组','其他','RX组','避孕用品','健康食品','情趣用品','隐形眼镜','计生试纸','器械组','中药饮片') and status in ('正常','停售','停购','禁售','停用') and waregeneralname like '%恩替卡韦片%'";
		 HashMap<String, Object> parameterMap = Maps.newHashMap();
		 parameterMap.put("ordersource", ordersource);
		 parameterMap.put("condition1", condition1);
		 parameterMap.put("condition2", condition2);
		 parameterMap.put("order", order);
		 
		 
		 List<WareSummaryEntity> goodsTableList = mapper.getGoodsTable(parameterMap);
		 int deleteCookieidWarecode = dao.deleteCookieidWarecode(cookieid);
		 System.out.println("删除记录行数=="+deleteCookieidWarecode);
		 dao.insertCookieidWarecode(cookieid,goodsTableList);
		 PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize), true, true, true);
		 PageInfo<WareSummaryEntity> page = new PageInfo<WareSummaryEntity>(mapper.getGoodsTable(parameterMap));
		 attributes.put("page", page);
		 attributes.put("allwarecode", goodsTableList);
		 j.setAttributes(attributes);
		 return j;
	 }
	 
	 
	 /**
	  * http://192.168.7.32:8080/waresanaysis/getManagerGoodsTable?loginname=mowanwen@360kad.com
	  * 商品经理搜索
	  * @param response
	  * @param pageNum
	  * @param pageSize
	  * @param ordersource
	  * @param waregeneralname
	  * @param brandname
	  * @param manufacturername
	  * @param suppliername
	  * @param warename
	  * @param cookieid
	  * @param condition1
	  * @param condition2
	  * @param order
	  * @param loginname
	  * @return
	  */
	 @RequestMapping("/getManagerGoodsTable")
	 @ResponseBody
	 public AjaxJson  getManagerGoodsTable(HttpServletResponse response,
			 @RequestParam(value = "pageNum", required = true) String pageNum,
			 @RequestParam(value = "pageSize", required = true) String pageSize,
			 @RequestParam(value = "ordersource", required = false) String ordersource,
			 @RequestParam(value = "waregeneralname",required = false)String waregeneralname,
			 @RequestParam(value = "brandname",required = false)String brandname,
			 @RequestParam(value = "manufacturername",required = false)String manufacturername,
			 @RequestParam(value = "suppliername",required = false)String suppliername,
			 @RequestParam(value = "warename",required = false)String warename,
			 @RequestParam(value = "warecode",required = false)String warecode,
			 @RequestParam(value = "cookieid",required = true)String cookieid,
			 @RequestParam(value = "condition1",required = false)String condition1,
			 @RequestParam(value = "condition2",required = false)String condition2,
			 @RequestParam(value = "order",required = false)String order,
			 @RequestParam(value = "loginname",required = true)String loginname
			 ){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 AjaxJson j = new AjaxJson();
		 Map<String, Object> attributes = Maps.newHashMap();
		 try {
			 
			 if(StringUtils.isBlank(order)){
				 order = "stockqty desc";
			 }
			 
			 if(StringUtils.isNotBlank(condition1)){
				 condition1 = "and expireddate - trunc(sysdate) <="+condition1;
			 }
			 if(StringUtils.isNotBlank(condition2)){
				 condition2 = java.net.URLDecoder.decode(condition2,"utf-8");
			 }
			 if(StringUtils.isNotBlank(waregeneralname)){
				 waregeneralname = java.net.URLDecoder.decode(waregeneralname,"utf-8");
				 condition2 = condition2+ " and waregeneralname like '%"+waregeneralname+"%'";
			 }
			 if(StringUtils.isNotBlank(brandname)){
				 brandname = java.net.URLDecoder.decode(brandname,"utf-8");
				 condition2 = condition2+ " and brandname like '%"+brandname+"%'";
			 }
			 if(StringUtils.isNotBlank(manufacturername)){
				 manufacturername = java.net.URLDecoder.decode(manufacturername,"utf-8");
				 condition2 = condition2+ " and manufacturername like '%"+manufacturername+"%'";
			 }
			 if(StringUtils.isNotBlank(warename)){
				 warename = java.net.URLDecoder.decode(warename,"utf-8");
				 condition2 = condition2+ " and warename like '%"+warename+"%'";
			 }
			 if(StringUtils.isNotBlank(suppliername)){
				 suppliername = java.net.URLDecoder.decode(suppliername,"utf-8");
				 condition2 = condition2+ " and suppliername like '%"+suppliername+"%'";
			 }
			 
		 } catch (UnsupportedEncodingException e) {
			 e.printStackTrace();
		 }
		
		 
		 /**
		  * 判断是否订阅
		  */
		 if(StringUtils.isNotEmpty(warename)||StringUtils.isNotEmpty(warecode)){
			 
			 HashMap<String, Object> countMap = Maps.newHashMap();
			 countMap.put("warename", warename);
			 countMap.put("warecode", warecode);
			 countMap.put("loginname", loginname);
			 int count = mapper.getManagerHave(countMap);
			 System.out.println("count=="+count);
			 if(count==0){
				 PageInfo page = new PageInfo();
				 page.setList(new ArrayList());
				 attributes.put("page",page);
				 attributes.put("allwarecode", null);
				 attributes.put("count", 0);
				 j.setAttributes(attributes);
				 return j;
			 }
		 }
		 
		 
//		 System.out.println("下面还要运行么？");
		 
		 HashMap<String, Object> parameterMap = Maps.newHashMap();
		 parameterMap.put("ordersource", ordersource);
		 parameterMap.put("condition1", condition1);
		 parameterMap.put("condition2", condition2);
		 parameterMap.put("order", order);
		 parameterMap.put("loginname", loginname);
		 
		 List<String> names = commonMapper.getNoFilterGoodsManager(loginname);
		 List<WareSummaryEntity> goodsTableList = null;
		 PageInfo<WareSummaryEntity> page =null;
		 if(names!=null && names.size()>0){
			 
			 System.out.println("该商品经理无需过滤商品*******");
			 goodsTableList = mapper.getNoFilterManagerGoodsTable(parameterMap);
			 System.out.println("goodsTableList="+goodsTableList.size());
			 
			 PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize), true, true, true);
			 page = new PageInfo<WareSummaryEntity>(mapper.getNoFilterManagerGoodsTable(parameterMap));
		 }else{
			 
			 System.out.println("需要过滤xxxxxxxxxxxxxxxxx");
			 goodsTableList = mapper.getManagerGoodsTable(parameterMap);
			 System.out.println("goodsTableList="+goodsTableList.size());
			 
			 PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize), true, true, true);
			 page = new PageInfo<WareSummaryEntity>(mapper.getManagerGoodsTable(parameterMap));
		 }
		 
		 int deleteCookieidWarecode = dao.deleteCookieidWarecode(cookieid);
		 System.out.println("删除记录行数=="+deleteCookieidWarecode);
		 dao.insertCookieidWarecode(cookieid,goodsTableList);
		 
		 
		 attributes.put("page", page);
		 attributes.put("allwarecode", goodsTableList);
		 attributes.put("count", 1);
		 j.setAttributes(attributes);
		 return j;
	 }
	 
	 /**
	  * http://192.168.7.32:8080/waresanaysis/selectWareCode?cookieid=&warecode
	  * @param response
	  * @param cookieid
	  * @param warecode
	  * @return
	  */
	 
	 @RequestMapping("/selectWareCode")
	 @ResponseBody
	 public AjaxJson selectWareCode(HttpServletResponse response,
			 @RequestParam(value = "cookieid",required = true)String cookieid,
			 @RequestParam(value = "warecode",required = true)String warecode
			 ){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 
		 List<String> warecodeList = Arrays.asList(warecode.split(","));
		 
		 System.err.println("selectWareCode cookieid========"+cookieid+"\t warecodeList="+warecodeList.size());
		 AjaxJson j = new AjaxJson();
		 int deleteCookieidWarecode = dao.deleteCookieidWarecode(cookieid);
		 System.out.println("xxx删除记录行数=="+deleteCookieidWarecode);
		 dao.insertCookieidWarecodeSelect(cookieid,warecodeList);
		 return j;
	 }

	 
	 /**
	  * http://192.168.7.32:8080/waresanaysis/getMonthOutQty?ordersource=&kpi=marketingcost_rate
	  * 出库数量 按月份
	  * @param response
	  * @param search
	  * @return
	  */
	@RequestMapping("/getMonthOutQty")
	@ResponseBody
	public AjaxJson getMonthOutQty(HttpServletResponse response,
			@RequestParam(value = "ordersource", required = false) String ordersource,
			@RequestParam(value = "kpi", required = true) String kpi,
			@RequestParam(value = "cookieid",required = true)String cookieid) {

//		购进数量(purinqty) 购进金额(purinamt) 总销量(sales_qty) 赠送销量(作为赠品)(sales_gift_qty) 赠送成本(sales_gift_cost)
//		净销量(net_sales_qty) 净销额(net_sales_amount)  收取运费(infreightcost) 销售成本(net_sales_cost) 搭配成本(marketingcost)  其他费(运费 包装费 cod费用 平台佣金)(othercost)
//		毛利额(gross_profit) 毛利率(gross_profit_rate) 净利额(net_gross_profit) 净利率(net_gross_profit_rate)
//		退货数量(return_qty) 退货率(return_rate) 销售价格(sales_price)
//		搭配占比(marketingcost_rate)  其他费用占比(othercost_rate)
		

		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");

		AjaxJson j = new AjaxJson();
		List<Object> yearArr = new ArrayList<Object>();
		Calendar now = Calendar.getInstance();
		now.set(Calendar.DATE, now.get(Calendar.DATE) - 1);
		int year = now.get(Calendar.YEAR);// 今年
//		yearArr.add(year - 3);
//		yearArr.add(year - 2);
		yearArr.add(year - 1);
		yearArr.add(year);
		
		int curmonth = now.get(Calendar.MONTH) + 1;//月份

		Map<String, Object> attributes = Maps.newHashMap();
		HashMap<String, Object> parameterMap = Maps.newHashMap();
		parameterMap.put("cookieid", cookieid);
		parameterMap.put("ordersource", ordersource);
		parameterMap.put("yearArr", yearArr);

		List<MonthOutRateEntity> monthOutRateList = null;
		if ("gross_profit_rate".equals(kpi.trim())) { //毛利率(gross_profit_rate)
			parameterMap.put("kpi", "gross_profit");
			parameterMap.put("divnum", "net_sales_amount");
			monthOutRateList = mapper.getMonthOutRate(parameterMap);
		} else if ("net_gross_profit_rate".equals(kpi.trim())) {//净利率(net_gross_profit_rate)
			parameterMap.put("kpi", "net_gross_profit");
			parameterMap.put("divnum", "net_sales_amount");
			monthOutRateList = mapper.getMonthOutRate(parameterMap);
		} else if ("sales_price".equals(kpi.trim())) {//销售价格(sales_price)
			parameterMap.put("kpi", "net_sales_amount");
			parameterMap.put("divnum", "net_sales_qty");
			monthOutRateList = mapper.getMonthOutRate(parameterMap);
		} else if ("return_rate".equals(kpi.trim())) {//退货率(return_rate) 
			parameterMap.put("kpi", "return_qty");
			parameterMap.put("divnum", "sales_qty");
			parameterMap.put("divadd", "return_qty");
			monthOutRateList = mapper.getMonthOutAddRate(parameterMap);

		}else if("marketingcost_rate".equals(kpi.trim())){ //搭配占比(marketingcost_rate)  
			parameterMap.put("kpi", "marketingcost");
			parameterMap.put("divnum", "net_sales_amount");
			monthOutRateList = mapper.getMonthOutRate(parameterMap);
		}else if("othercost_rate".equals(kpi.trim())){ //其他费用占比(othercost_rate)
			parameterMap.put("kpi", "othercost");
			parameterMap.put("divnum", "net_sales_amount");
			monthOutRateList = mapper.getMonthOutRate(parameterMap);
		}else if("purinqty".equals(kpi.trim()) || "purinamt".equals(kpi.trim())){//购进数量(purinqty) 购进金额(purinamt)
			parameterMap.put("kpi", kpi);
			monthOutRateList = mapper.getMonthInput(parameterMap);
		} else {
			parameterMap.put("kpi", kpi);
			monthOutRateList = mapper.getMonthOutQty(parameterMap);
		}
		attributes.put("monthOutQty", monthOutRateList);
		List<Object> currList = new ArrayList<Object>();
		List<Object> lastList = new ArrayList<Object>();
		String[] months = { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" };
		for (MonthOutRateEntity entity : monthOutRateList) {
			if (year == entity.getYear()) {
				currList = dao.getMonthkpiValue(entity,curmonth);
			} else if((year-1) ==entity.getYear()) {
				lastList = dao.getMonthkpiValue(entity);
			}
		}
		Map<String, Object> directesult = Maps.newHashMap();
		directesult.put("legend", yearArr);
		directesult.put("xAxis", months);
		directesult.put("last_yvalue", lastList);
		directesult.put("curr_yvalue", currList);

		attributes.put("monthOutQtyDirect", directesult);
		j.setAttributes(attributes);
		return j;
	}
	 
	 /**
	  * http://192.168.7.32:8080/waresanaysis/getDayOutQty?ordersource=&kpi=gross_profit_rate
	  * 出库数量 按天
	  * @param response
	  * @param search
	  * @return
	  */
	 @RequestMapping("/getDayOutQty")
	 @ResponseBody
	 public AjaxJson getDayOutQty(HttpServletResponse response,
			 @RequestParam(value = "ordersource",required = false)String ordersource,
			 @RequestParam(value = "kpi",required = true)String kpi,
			 @RequestParam(value = "cookieid",required = true)String cookieid
			 ){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 
		 
		 AjaxJson j = new AjaxJson();
		 List<Object> yearArr = new ArrayList<Object>();
		 
		 Calendar now=Calendar.getInstance();
		 int year = now.get(Calendar.YEAR); //当前年
		 now.set(Calendar.DATE, now.get(Calendar.DATE) - 1);
		 yearArr.add(year-1);
		 yearArr.add(year);
		 int curday = now.get(Calendar.DATE);//当前日
//		 
		 
		 Map<String, Object> attributes = Maps.newHashMap();
		 HashMap<String, Object> parameterMap = Maps.newHashMap();
		 parameterMap.put("cookieid", cookieid);
		 parameterMap.put("ordersource",ordersource);
		 parameterMap.put("yearArr",yearArr);
		 
		 long querystart = new Date().getTime();
		 List<DayOutRateEntity> dayOutQtyList = null;
		 if("gross_profit_rate".equals(kpi.trim())){
			 parameterMap.put("kpi","gross_profit");
			 parameterMap.put("divnum","net_sales_amount");
			 dayOutQtyList = mapper.getDayOutRate(parameterMap);
		 }else if("net_gross_profit_rate".equals(kpi.trim())){
			 parameterMap.put("kpi","net_gross_profit");
			 parameterMap.put("divnum","net_sales_amount");
			 dayOutQtyList = mapper.getDayOutRate(parameterMap);
		 } else if("sales_price".equals(kpi.trim())){
			 parameterMap.put("kpi","net_sales_amount");
			 parameterMap.put("divnum","net_sales_qty");
			 dayOutQtyList = mapper.getDayOutRate(parameterMap);
		 }else if("return_rate".equals(kpi.trim())){
			parameterMap.put("kpi", "return_qty");
			parameterMap.put("divnum", "sales_qty");
			parameterMap.put("divadd", "return_qty");
			 dayOutQtyList = mapper.getDayOutAddRate(parameterMap);
			 
		 }else if("marketingcost_rate".equals(kpi.trim())){
			parameterMap.put("kpi", "marketingcost");
			parameterMap.put("divnum", "net_sales_amount");
			dayOutQtyList = mapper.getDayOutRate(parameterMap);
		}else if("othercost_rate".equals(kpi.trim())){
			parameterMap.put("kpi", "othercost");
			parameterMap.put("divnum", "net_sales_amount");
			dayOutQtyList = mapper.getDayOutRate(parameterMap);
		}else if("purinqty".equals(kpi.trim()) || "purinamt".equals(kpi.trim())){//购进数量(purinqty) 购进金额(purinamt)
			parameterMap.put("kpi", kpi);
			dayOutQtyList = mapper.getDayInput(parameterMap);
		}else{
			parameterMap.put("kpi",kpi);
			dayOutQtyList = mapper.getDayOutQty(parameterMap);
		 }
		 attributes.put("dayOutQty", dayOutQtyList);
		 
		 System.err.println("查询数据库花费时间"+(new Date().getTime()-querystart));
		 String[] days = { "01","02","03","04","05","06","07","08","09","10",
		    	   "11","12","13","14","15","16","17","18","19","20",
		    	   "21","22","23","24","25","26","27","28","29","30","31"};
		 
		 List<Object> legend=new ArrayList<Object>();
		 List<Float> currList=new ArrayList<Float>();
		 List<Float> lastList=new ArrayList<Float>();
		 
		 if(dayOutQtyList!=null && dayOutQtyList.size()>=2){
			 DayOutRateEntity curEntity = dayOutQtyList.get(0);
			 DayOutRateEntity lastEntity = dayOutQtyList.get(1);
			 legend.add(lastEntity.getData_mth());
			 legend.add(curEntity.getData_mth());
			 currList = dao.getDaykpiValue(curEntity,curday);
			 lastList = dao.getDaykpiValue(lastEntity);
		 }else if(dayOutQtyList!=null && dayOutQtyList.size()==1){
			 DayOutRateEntity curEntity = dayOutQtyList.get(0);
			 legend.add(curEntity.getData_mth());
			 currList = dao.getDaykpiValue(curEntity,curday);
		 }
		 Map<String,Object> directesult = Maps.newHashMap(); 
		 directesult.put("legend", legend);
		 directesult.put("xAxis", days);
		 directesult.put("last_yvalue", lastList);
		 directesult.put("curr_yvalue", currList);
		 attributes.put("DayOutQtyDirect", directesult);
		 j.setAttributes(attributes);
		 return j;
	 }
	 
	 /**
	  * http://192.168.7.32:8080/waresanaysis/getStockQty?
	  * 库存趋势
	  * @param response
	  * @param waregeneralname
	  * @param brandname
	  * @param manufacturername
	  * @param warecode
	  * @param warename
	  * @param ordersource
	  * @param kpi
	  * @return
	  */
	 @RequestMapping("/getStockQty")
	 @ResponseBody
	 public AjaxJson  getStockQty(HttpServletResponse response,
			 @RequestParam(value = "cookieid",required = true)String cookieid
			 ){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 
		 HashMap<String, Object> parameterMap = Maps.newHashMap();
		 parameterMap.put("cookieid", cookieid);
		 
		 AjaxJson j = new AjaxJson();
		 Map<String, Object> attributes = Maps.newHashMap();
		 
		 
		 Map<String,Object> stockqtyret = Maps.newHashMap(); 
		 Map<String,Object> stockamtret = Maps.newHashMap(); 
		 String[] stockqtylegend={"库存数量"};
		 String[] stockamtlegend={"库存金额"};
		 List<String> days=new ArrayList<String>();
		 List<Integer> stockqtyList=new ArrayList<Integer>();
		 List<Integer> stockamtList=new ArrayList<Integer>();
		 List<WaresAnalyseDay> stockQty = mapper.getStockQty(parameterMap);
		
		 for (WaresAnalyseDay entity : stockQty) {
			 days.add(entity.getDataday());
			 stockamtList.add(entity.getLine1());
			 stockqtyList.add(entity.getLine2());
		 }
		 
		 stockqtyret.put("legend", stockqtylegend);
		 stockqtyret.put("xAxis", days);
		 stockqtyret.put("yAxis", stockqtyList);
		 attributes.put("stockqtyList", stockqtyret);
		 
		 stockamtret.put("legend", stockamtlegend);
		 stockamtret.put("xAxis", days);
		 stockamtret.put("yAxis", stockamtList);
		 attributes.put("stockamtList", stockamtret);
		 j.setAttributes(attributes);
		 return j;
	 }
	 
	 
	 /**
	  * 
	  * http://192.168.7.32:8080/waresanaysis/getBuyInput?cookieid=1bcfabe8-f977-4a60-b9ce-8029ff8d29b4
	  * 购进
	  * @param response
	  * @param waregeneralname
	  * @param brandname
	  * @param manufacturername
	  * @param warecode
	  * @param warename
	  * @return
	  */
	 @RequestMapping("/getBuyInput")
	 @ResponseBody
	 public AjaxJson  getBuyInput(HttpServletResponse response,
			 @RequestParam(value = "startdate",required = false)String startdate,
			 @RequestParam(value = "enddate",required = false)String enddate,
			 @RequestParam(value = "cookieid",required = true)String cookieid
			 ){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 if(StringUtils.isBlank(startdate)){
			 startdate = DvDateUtils.format(DvDateUtils.getOffsetMonth(new Date(), -12), "yyyy-MM-dd");
		 }
		 if(StringUtils.isBlank(enddate)){
			 enddate = DvDateUtils.format(new Date(), "yyyy-MM-dd");
		 }
		 
		 
		 
		 AjaxJson j = new AjaxJson();
		 Map<String, Object> attributes = Maps.newHashMap();
		 
		 HashMap<String, Object> parameterMap = Maps.newHashMap();
		 
		 parameterMap.put("cookieid", cookieid);
		 parameterMap.put("startdate", startdate);
		 parameterMap.put("enddate", enddate);
		 List<BuyInputEntity> buyInputList = mapper.getBuyInput(parameterMap);
		 attributes.put("buyInput", buyInputList); 

		 j.setAttributes(attributes);
		 return j;
	 }
	 
	 /**
	  * 
	  * 购进  右边曲线图
	  * @param response
	  * @param waregeneralname
	  * @param brandname
	  * @param manufacturername
	  * @param warecode
	  * @param warename
	  * @param suppliername
	  * @return
	  */
	 @RequestMapping("/getBuyIncountAmount")
	 @ResponseBody
	 public AjaxJson getBuyInputClickSupplier(HttpServletResponse response,
			 @RequestParam(value = "suppliername",required = false)String suppliername,
			 @RequestParam(value = "cookieid",required = true)String cookieid
			 ){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 AjaxJson j = new AjaxJson();
		 Map<String, Object> attributes = Maps.newHashMap();
		 
		 
		 HashMap<String, Object> parameterMap = Maps.newHashMap();
		 parameterMap.put("cookieid", cookieid);
		 parameterMap.put("suppliername", suppliername);
		 List<BuyInputEntity> buyInputList = mapper.getBuyIncountAmount(parameterMap);
		 
		 String[] incountlegend={"购进数量"};
		 String[] inamountlegend={"购进金额"};
		 
		 List<Integer> incountList=new ArrayList<Integer>();
		 List<Integer> inamountList=new ArrayList<Integer>();
		 List<String> dayList=new ArrayList<String>();
		 for (BuyInputEntity entity : buyInputList) {
			 dayList.add(entity.getSuppliername());//代表日期 因为不想再新建实体类 就跟上面一个方法用的同一个实体类
			 incountList.add(entity.getIncount());
			 inamountList.add(entity.getInamount());
			 
		 }
		 Map<String,Object> incountChart = Maps.newHashMap(); 
		 incountChart.put("xAxis", dayList);
		 incountChart.put("legend", incountlegend);
		 incountChart.put("yAxis", incountList);
		 attributes.put("incountChart", incountChart);
		 
		 Map<String,Object> inamountChart = Maps.newHashMap(); 
		 inamountChart.put("xAxis", dayList);
		 inamountChart.put("legend", inamountlegend);
		 inamountChart.put("yAxis", inamountList);
		 attributes.put("inamountChart", inamountChart);
		 
		 j.setAttributes(attributes);
		 return j;
	 }
	 
	 /**
	  * http://192.168.7.32:8080/waresanaysis/getBuyIncountAmountTable?cookieid=&suppliername=
	  * @param response
	  * @param suppliername
	  * @param cookieid
	  * @return
	  */
	 @RequestMapping("/getBuyIncountAmountTable")
	 @ResponseBody
	 public AjaxJson getBuyIncountAmountTable(HttpServletResponse response,
			 @RequestParam(value = "suppliername",required = false)String suppliername,
			 @RequestParam(value = "cookieid",required = true)String cookieid
			 ){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 
		 try {
			 if(StringUtils.isNotBlank(suppliername)){
				 suppliername = java.net.URLDecoder.decode(suppliername,"utf-8");
			 }
		 } catch (UnsupportedEncodingException e) {
			 e.printStackTrace();
		 }
		 
		 AjaxJson j = new AjaxJson();
		 Map<String, Object> attributes = Maps.newHashMap();
		 HashMap<String, Object> parameterMap = Maps.newHashMap();
		 parameterMap.put("cookieid", cookieid);
		 parameterMap.put("suppliername", suppliername);
		 List<YearBuyInputEntity> buyInputList = mapper.getBuyIncountAmountTable(parameterMap);
		 attributes.put("buyInputList", buyInputList);
		 j.setAttributes(attributes);
		 return j;
	 }
	 
	 /**
	  * 
	  * 供应商采购信息
	  * @param response
	  * @param waregeneralname
	  * @param brandname
	  * @param manufacturername
	  * @param warecode
	  * @param warename
	  * @return
	  */
	 @RequestMapping("/getSupplierInput")
	 @ResponseBody
	 public AjaxJson  getSupplierInput(HttpServletResponse response,
			 @RequestParam(value = "warecode",required = true)String warecode,
			 @RequestParam(value = "suppliername",required = false)String suppliername,
			 @RequestParam(value = "kpi",required = true)String kpi//incount,inamount,avg_inprice
			 ){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 AjaxJson j = new AjaxJson();
		 List<Object> attributes = new ArrayList<Object>();
		 
		 try {
			 if(StringUtils.isNotBlank(suppliername)){
				 suppliername = java.net.URLDecoder.decode(suppliername,"utf-8");
			 }
		 } catch (UnsupportedEncodingException e) {
			 e.printStackTrace();
		 }
		 
		 HashMap<String, Object> parameterMap = Maps.newHashMap();
		 parameterMap.put("warecode", warecode);
		 parameterMap.put("suppliername", suppliername);
		 parameterMap.put("kpi", kpi);
		 List<SupplierInputEntity> supplierInputList = mapper.getSupplierInput(parameterMap);
		 Set<String> supplierSet = new HashSet<String>();
		
		Set<String> daySet = new TreeSet<String>(new Comparator<String>() {
			@Override
			public int compare(String obj0, String obj1) {
				return obj1.compareTo(obj0);
			}
		});
		 
		 for (SupplierInputEntity entity : supplierInputList) {
			 supplierSet.add(entity.getSuppliername());
			 daySet.add(entity.getDataday());
			 
		 }
		 
//		 System.err.println("供应商数量=="+supplierSet.size());
//		 incount,inamount,avg_inprice
		 if(kpi.equals("incount")){
			 String[] incountlegend={"购进数量"};
			 for (String supp : supplierSet) {
//				Map<String,Object> incountMap = new TreeMap<String,Object>();
				Map<String, Object> incountMap = new TreeMap<String, Object>(new Comparator<String>() {
					public int compare(String obj0, String obj1) {
						// 降序排序
						return obj1.compareTo(obj0);
					}
				});
//				 System.err.println("当前供应商为"+supp);
				 List<Object> incountList=new ArrayList<Object>();
				 for (SupplierInputEntity entity : supplierInputList) {
					 incountMap.put(entity.getDataday(), "-");
					 if(supp.equals(entity.getSuppliername())){
//						 incountList.add(entity.getIncount());
						 incountMap.put(entity.getDataday(), entity.getIncount());
					 }
				 }
//				 for (Map.Entry<String, Object> entry : incountMap.entrySet()) {
//					 System.out.println(supp+"  Key = " + entry.getKey() + ", Value = " + entry.getValue());  
//				 }
				 incountList = new ArrayList<Object>(incountMap.values());     
				 Map<String,Object> incountChart = Maps.newHashMap(); 
				 incountChart.put("xAxis", daySet);
				 incountChart.put("legend", incountlegend);
				 incountChart.put("yAxis", incountList);
				 incountChart.put("name", supp.replace("有限公司", ""));
				 attributes.add(incountChart);
//				 attributes.put(supp, incountChart);
			 }
		 }else if(kpi.equals("inamount")){
			 String[] inamountlegend={"购进金额"};
			 for (String supp : supplierSet) {
				Map<String, Object> amountMap = new TreeMap<String, Object>(new Comparator<String>() {
					public int compare(String obj0, String obj1) {
						// 降序排序
						return obj1.compareTo(obj0);
					}
				});
				 List<Object> inamountList=new ArrayList<Object>();
				 for (SupplierInputEntity entity : supplierInputList) {
					 amountMap.put(entity.getDataday(), "-");
					 if(supp.equals(entity.getSuppliername())){
						 amountMap.put(entity.getDataday(), entity.getInamount());
					 }
				 }
				 
				 inamountList = new ArrayList<Object>(amountMap.values());     
				 Map<String,Object> inamountChart = Maps.newHashMap(); 
				 inamountChart.put("xAxis", daySet);
				 inamountChart.put("legend", inamountlegend);
				 inamountChart.put("yAxis", inamountList);
				 inamountChart.put("name", supp.replace("有限公司", ""));
				 attributes.add(inamountChart);
			 }
			 
		 }else if(kpi.equals("avg_inprice")){
			 String[] avginpricelegend={"购进价格"};
			 for (String supp : supplierSet) {
				 
				Map<String, Object> priceMap = new TreeMap<String, Object>(new Comparator<String>() {
					public int compare(String obj0, String obj1) {
						// 降序排序
						return obj1.compareTo(obj0);
					}
				});
				 List<Object> avginpriceList=new ArrayList<Object>();
				 for (SupplierInputEntity entity : supplierInputList) {
					 priceMap.put(entity.getDataday(), "-");
					 if(supp.equals(entity.getSuppliername())){
						 priceMap.put(entity.getDataday(), entity.getAvg_inprice());
					 }
				 }
				 
				 avginpriceList = new ArrayList<Object>(priceMap.values());  
				 Map<String,Object> avginpriceChart = Maps.newHashMap(); 
				 avginpriceChart.put("xAxis", daySet);
				 avginpriceChart.put("legend", avginpricelegend);
				 avginpriceChart.put("yAxis", avginpriceList);
				 avginpriceChart.put("name", supp.replace("有限公司", ""));
				 attributes.add(avginpriceChart);
							 
		    }
		 }
		 j.setObj(attributes);
		 return j;
	 }
	 	 
	 /**
	  * 下拉列表数据
	  * @param response
	  * @param waregeneralname
	  * @param brandname
	  * @param manufacturername
	  * @param warecode
	  * @param warename
	  * @param suppliername
	  * @return
	  */
	 @RequestMapping("/getSelectWareName")
	 @ResponseBody
	 public AjaxJson getSelectWareName(HttpServletResponse response,
			 @RequestParam(value = "suppliername",required = false)String suppliername,
			 @RequestParam(value = "cookieid",required = true)String cookieid
			 ){
		
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 
		 try {
			 if(StringUtils.isNotBlank(suppliername)){
				 suppliername = java.net.URLDecoder.decode(suppliername,"utf-8");
			 }
		 } catch (UnsupportedEncodingException e) {
			 e.printStackTrace();
		 }
		 
//		 System.out.println("suppliername=="+suppliername);
		 AjaxJson j = new AjaxJson();
		 Map<String, Object> attributes = Maps.newHashMap();
		 HashMap<String, Object> parameterMap = Maps.newHashMap();
		 parameterMap.put("cookieid", cookieid);
		 parameterMap.put("suppliername", suppliername);
		 List<SelectWareNameEntity> selectWareNameList = mapper.getSelectWareName(parameterMap);
		 attributes.put("selectWareName", selectWareNameList);
		 j.setAttributes(attributes);
		 return j;
	 }
	 
	/**
	 * 
	 * 搭配商品排行
	 * http://192.168.7.32:8080/waresanaysis/visWareRankPage?pageNum=1
	 * @param response
	 * @param pageNum
	 * @param pageSize
	 * @param waregeneralname
	 * @param brandname
	 * @param manufacturername
	 * @param warecode
	 * @param warename
	 * @return
	 */
	@RequestMapping("/visWareRankPage")
	@ResponseBody
	public PageInfo<VisWareRankEntity> visWareRankPage(HttpServletResponse response,
			@RequestParam(value = "pageNum", required = true) String pageNum,
			@RequestParam(value = "pageSize", required = true) String pageSize,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate,
			@RequestParam(value = "cookieid",required = true)String cookieid,
			@RequestParam(value = "order",required = false)String order) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		if (StringUtils.isBlank(startdate)) {
			startdate = DvDateUtils.format(DvDateUtils.getOffsetMonth(new Date(), -1), "yyyy-MM-dd");
		}
		if (StringUtils.isBlank(enddate)) {
			enddate = DvDateUtils.format(new Date(), "yyyy-MM-dd");
		}
		
		
		 if(StringUtils.isBlank(order)){
			 order = "salescost desc";
		 }
		 
		HashMap<String, Object> parameterMap = Maps.newHashMap();
		parameterMap.put("cookieid", cookieid);
		parameterMap.put("startdate", startdate);
		parameterMap.put("enddate", enddate);
		parameterMap.put("order", order);
		PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize), true, true, true);
		PageInfo<VisWareRankEntity> page = new PageInfo<VisWareRankEntity>(mapper.visWareRankPage(parameterMap));
		return page;
	}
	
	/**
	 * http://192.168.7.32:8080/waresanaysis/visMainWareRankPage
	 * 主品排行
	 * @param response
	 * @param pageNum
	 * @param pageSize
	 * @param startdate
	 * @param enddate
	 * @param cookieid
	 * @param order
	 * @return
	 */
	@RequestMapping("/visMainWareRankPage")
	@ResponseBody
	public PageInfo<VisWareRankEntity> visMainWareRankPage(HttpServletResponse response,
			@RequestParam(value = "pageNum", required = true) String pageNum,
			@RequestParam(value = "pageSize", required = true) String pageSize,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate,
			@RequestParam(value = "cookieid",required = true)String cookieid,
			@RequestParam(value = "order",required = false)String order) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		if (StringUtils.isBlank(startdate)) {
			startdate = DvDateUtils.format(DvDateUtils.getOffsetMonth(new Date(), -1), "yyyy-MM-dd");
		}
		if (StringUtils.isBlank(enddate)) {
			enddate = DvDateUtils.format(new Date(), "yyyy-MM-dd");
		}
		
		
		if(StringUtils.isBlank(order)){
			order = "salescost desc";
		}
		
		HashMap<String, Object> parameterMap = Maps.newHashMap();
		parameterMap.put("cookieid", cookieid);
		parameterMap.put("startdate", startdate);
		parameterMap.put("enddate", enddate);
		parameterMap.put("order", order);
		long time1 = new Date().getTime();
		PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize), true, true, true);
		PageInfo<VisWareRankEntity> page = new PageInfo<VisWareRankEntity>(mapper.visMainWareRankPage(parameterMap));
		System.out.println("visMainWareRankPage time = "+(new Date().getTime()-time1));
		return page;
	}
	
	
	/**
	 * http://192.168.7.32:8080/waresanaysis/visWareRankTotal?cookieid=b73a6a54-036b-452c-8f40-87b7d8b3cb87&startdate=2016-01-01&enddate=2016-12-27
	 * @param response
	 * @param startdate
	 * @param enddate
	 * @param cookieid
	 * @return
	 */
	@RequestMapping("/visWareRankTotal")
	@ResponseBody
	public AjaxJson visWareRankTotal(HttpServletResponse response,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate,
			@RequestParam(value = "cookieid",required = true)String cookieid) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		AjaxJson j = new AjaxJson();

		Map<String, Object> attributes = Maps.newHashMap();
		
		System.out.println(startdate+"\t"+enddate+"\t"+cookieid);
		
		if (StringUtils.isBlank(startdate)) {
			startdate = DvDateUtils.format(DvDateUtils.getOffsetMonth(new Date(), -1), "yyyy-MM-dd");
		}
		if (StringUtils.isBlank(enddate)) {
			enddate = DvDateUtils.format(new Date(), "yyyy-MM-dd");
		}
		HashMap<String, Object> parameterMap = Maps.newHashMap();
		parameterMap.put("cookieid", cookieid);
		parameterMap.put("startdate", startdate);
		parameterMap.put("enddate", enddate);
		long time1 = new Date().getTime();
		VisWareRankTotalEntity visWareRankTotal = mapper.visWareRankTotal(parameterMap);
		
		System.out.println("visWareRankTotal time = "+(new Date().getTime()-time1));
		System.out.println("my cookieid = "+cookieid);
		if(visWareRankTotal!=null){
			
			System.out.println(cookieid+"合计= "+visWareRankTotal.getSalescost()+"\t"+visWareRankTotal.getSalesqty());
		}else{
			System.out.println(cookieid+"合计 is null");
		}
		attributes.put("visWareRankTotal", visWareRankTotal);
		j.setAttributes(attributes);
		return j;
	}
	/**
	 * http://192.168.7.32:8080/waresanaysis/mixWareRankPage?pageNum=0&pageSize=10&cookieid=b73a6a54-036b-452c-8f40-87b7d8b3cb87&startdate=2016-01-01&enddate=2016-12-27
	 * @param response
	 * @param pageNum
	 * @param pageSize
	 * @param waregeneralname
	 * @param brandname
	 * @param manufacturername
	 * @param warecode
	 * @param warename
	 * @return
	 */
	@RequestMapping("/mixWareRankPage")
	@ResponseBody
	public PageInfo<VisWareRankEntity> mixWareRankPage(HttpServletResponse response,
			@RequestParam(value = "pageNum", required = true) String pageNum,
			@RequestParam(value = "pageSize", required = true) String pageSize,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate,
			@RequestParam(value = "cookieid",required = true)String cookieid,
			@RequestParam(value = "order",required = false)String order) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		if (StringUtils.isBlank(startdate)) {
			startdate = DvDateUtils.format(DvDateUtils.getOffsetMonth(new Date(), -1), "yyyy-MM-dd");
		}
		if (StringUtils.isBlank(enddate)) {
			enddate = DvDateUtils.format(new Date(), "yyyy-MM-dd");
		}
		if(StringUtils.isBlank(order)){
			 order = "salesqty desc";
		 }
		 
		HashMap<String, Object> parameterMap = Maps.newHashMap();
		parameterMap.put("cookieid", cookieid);
		parameterMap.put("startdate", startdate);
		parameterMap.put("enddate", enddate);
		parameterMap.put("order", order);
		
		long time1 = new Date().getTime();
		PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize), true, true, true);
		PageInfo<VisWareRankEntity> page = new PageInfo<VisWareRankEntity>(mapper.mixWareRankPage(parameterMap));
		
		System.out.println("mixWareRankPage time = "+(new Date().getTime()-time1));
		return page;
	}
	
	/**
	 * http://192.168.7.32:8080/waresanaysis/mixWareRankDetail?mixcode=10000258810,113906,114404,38998,73616&startdate=&enddate=
	 * @param response
	 * @param mixcode
	 * @param startdate
	 * @param enddate
	 * @return
	 */
	@RequestMapping("/mixWareRankDetail")
	@ResponseBody
	public AjaxJson mixWareRankDetail(HttpServletResponse response,
			@RequestParam(value = "mixcode", required = true) String mixcode,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");

		if (StringUtils.isBlank(startdate)) {
			startdate = DvDateUtils.format(DvDateUtils.getOffsetMonth(new Date(), -12), "yyyy-MM-dd");
		}
		if (StringUtils.isBlank(enddate)) {
			enddate = DvDateUtils.format(new Date(), "yyyy-MM-dd");
		}

		AjaxJson j = new AjaxJson();

		Map<String, Object> attributes = Maps.newHashMap();
		HashMap<String, Object> parameterMap = Maps.newHashMap();
		parameterMap.put("mixcode", mixcode);
		parameterMap.put("startdate", startdate);
		parameterMap.put("enddate", enddate);
		List<VisWareRankEntity> mixWareRankDetailList = mapper.mixWareRankDetail(parameterMap);
		attributes.put("mixWareRankDetail", mixWareRankDetailList);
		j.setAttributes(attributes);
		return j;
	}

	/**
	 * 
	 * http://192.168.7.32:8080/waresanaysis/getMonthMatchLine?waregeneralname=&brandname=&manufacturername=&warecode=80095&warename=
	 * 搭配 年度趋势
	 * @param response
	 * @param waregeneralname
	 * @param brandname
	 * @param manufacturername
	 * @param warecode
	 * @param warename
	 * @return
	 */
	@RequestMapping("/getMonthMatchLine")
	@ResponseBody
	public AjaxJson getMonthMatchLine(HttpServletResponse response,
			@RequestParam(value = "cookieid",required = true)String cookieid) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");


		AjaxJson j = new AjaxJson();

		Map<String, Object> attributes = Maps.newHashMap();
		HashMap<String, Object> parameterMap = Maps.newHashMap();
		parameterMap.put("cookieid", cookieid);
		
		List<MatchLinesEntity> monthMatchLine = mapper.getMonthMatchLine(parameterMap);

		String[] mthlegend = { "搭配成本", "搭配成本占比" };
		List<Float> marketingcostList = new ArrayList<Float>();
		List<Float> rateList = new ArrayList<Float>();
		List<String> monthList = new ArrayList<String>();
		for (MatchLinesEntity entity : monthMatchLine) {
			String mth = entity.getData_time();
			if (mth.startsWith("0")) {
				mth = mth.substring(1);
			}
			monthList.add(mth);
			marketingcostList.add(entity.getMarketingcost());
			rateList.add(entity.getRate());
		}
		Map<String, Object> mthChart = Maps.newHashMap();
		mthChart.put("xAxis", monthList);
		mthChart.put("legend", mthlegend);
		mthChart.put("marketingcost", marketingcostList);
		mthChart.put("rate", rateList);
		attributes.put("mthChart", mthChart);
		j.setAttributes(attributes);
		return j;
	}
	
	/**
	 * http://192.168.7.32:8080/waresanaysis/getDayMatchLine?waregeneralname=&brandname=&manufacturername=&warecode=80095&warename=
	 * 搭配 月度趋势
	 * @param response
	 * @param waregeneralname
	 * @param brandname
	 * @param manufacturername
	 * @param warecode
	 * @param warename
	 * @return
	 */
	@RequestMapping("/getDayMatchLine")
	@ResponseBody
	public AjaxJson getDayMatchLine(HttpServletResponse response,
			@RequestParam(value = "cookieid",required = true)String cookieid) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		
		AjaxJson j = new AjaxJson();
		
		Map<String, Object> attributes = Maps.newHashMap();
		HashMap<String, Object> parameterMap = Maps.newHashMap();
		parameterMap.put("cookieid", cookieid);
		
		List<MatchLinesEntity> dayMatchLine = mapper.getDayMatchLine(parameterMap);
		
		String[] daylegend = { "搭配成本", "搭配成本占比" };
		List<Float> marketingcostList = new ArrayList<Float>();
		List<Float> rateList = new ArrayList<Float>();
		List<String> dayList = new ArrayList<String>();
		for (MatchLinesEntity entity : dayMatchLine) {
			dayList.add(entity.getData_time());
			marketingcostList.add(entity.getMarketingcost());
			rateList.add(entity.getRate());
		}
		Map<String, Object> dayChart = Maps.newHashMap();
		dayChart.put("xAxis", dayList);
		dayChart.put("legend", daylegend);
		dayChart.put("marketingcost", marketingcostList);
		dayChart.put("rate", rateList);
		attributes.put("dayChart", dayChart);
		j.setAttributes(attributes);
		return j;
	}
	
	
	/**
	 * http://192.168.7.32:8080/waresanaysis/exportExcel?startdate=2016-01-01&enddate=2016-12-12&fileName=sigle&cookieid=cacd3f42-eefc-4095-ae3d-c3d3b9168611
	 * @param response
	 * @param startdate
	 * @param enddate
	 * @param cookieid
	 * @param fileName
	 */
	@RequestMapping("/exportExcel")
    public void exportMatchWare(HttpServletResponse response,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate,
			@RequestParam(value = "cookieid",required = true)String cookieid,
    		@RequestParam(value = "fileName", required = false) String fileName){
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("octets/stream");
    	String formatDate = DvDateUtils.format(new Date(), "yyyyMMddHHmmss");
    	Map<String,Object> mapParameter=new HashMap<String, Object>();
    	mapParameter.put("startdate", startdate);
    	mapParameter.put("enddate", enddate);
    	mapParameter.put("cookieid", cookieid);
    	mapParameter.put("order", "salescost desc");
    	System.out.println(startdate+enddate+cookieid);
    	
    	int tableFlag=0;
    	if("sigle".equals(fileName)){
    		fileName = "搭配商品排行_"+formatDate;
    		tableFlag=1;
    	}else if("mix".equals(fileName)){
    		fileName = "搭配组合排行_"+formatDate;
    		tableFlag=2;
    	}else if("main".equals(fileName)){
    		fileName = "主商品排行_"+formatDate;
    		tableFlag=3;
    	}
    	System.out.println("fileName=="+fileName);
        OutputStream out=null;
		try {
			String codedFileName = java.net.URLEncoder.encode(fileName,"UTF-8");
			response.addHeader("Content-Disposition", "attachment;filename="+codedFileName+".xls");
			out = response.getOutputStream();
			
			WritableWorkbook workbook = Workbook.createWorkbook(out);
			if (tableFlag==1) {
				List<VisWareRankEntity> sigleRankWare = mapper.visWareRankPage(mapParameter);
				String[] titles={"商品编码","商品名称","搭配数量","搭配成本","占比"};
				String[] fields={"warecode","warename","salesqty","salescost","salescost_rate"};
				ExportExcel<VisWareRankEntity> export = new ExportExcel<VisWareRankEntity>();
				export.fillExcel(workbook, VisWareRankEntity.class,
						titles, fields,
						"搭配商品排行数据", sigleRankWare);
			}else if(tableFlag==2) {
				List<VisWareRankEntity> mixRankWare = mapper.mixWareRankDetailExcel(mapParameter);
				String[] titles={"商品组合","商品编码","商品名称","是否主品","销量","销额","成本","占比"};
				String[] fields={"mixcode","warecode","warename","ismain","salesqty","salesamt","salescost","salescost_rate"};
				ExportExcel<VisWareRankEntity> export = new ExportExcel<VisWareRankEntity>();
				export.fillExcel(workbook, VisWareRankEntity.class,
						titles, fields,
						"搭配组合排行数据", mixRankWare);
			}else if(tableFlag==3) {
				System.out.println("主商品排行导入excel");
				List<VisWareRankEntity> mainRankWare = mapper.visMainWareRankPage(mapParameter);
				String[] titles={"商品编码","商品名称","销量","销额","占比"};
				String[] fields={"warecode","warename","salesqty","salescost","salescost_rate"};
				ExportExcel<VisWareRankEntity> export = new ExportExcel<VisWareRankEntity>();
				export.fillExcel(workbook, VisWareRankEntity.class,
						titles, fields,
						"主商品排行数据", mainRankWare);
			}
			workbook.write();
			workbook.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    	
    }
}
